﻿using System;
using System.Configuration;
using System.ServiceModel.Configuration;

namespace Sripirom.ATS.Core.ServiceModel.Description
{
    public class AtsEndpointBehaviorExtensionElement : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof(AtsEndpointBehavior); }
        }

        protected override object CreateBehavior()                  
        {
            
            return new AtsEndpointBehavior(PoolingProvider,  MessageInspector, ErrorHandler );
        }

       
        #region config Attribute
        [ConfigurationProperty("poolingProvider", DefaultValue = null, IsRequired = false, IsKey = false)]
        public PoolingProviderConfigElement PoolingProvider
        {
            get { return (PoolingProviderConfigElement)this["poolingProvider"]; }
            set { this["poolingProvider"] = value; }                                    
        }

        [ConfigurationProperty("messageInspector", DefaultValue = null, IsRequired = false, IsKey = false)]
        public DispatchMessageInspectorConfigElement MessageInspector
        {
            get { return (DispatchMessageInspectorConfigElement)this["messageInspector"]; }
            set { this["messageInspector"] = value; }
        }

        [ConfigurationProperty("errorHandler", DefaultValue = null, IsRequired = true, IsKey = false)]
        public ErrorHandlerConfigElement ErrorHandler
        {
            get { return (ErrorHandlerConfigElement)this["errorHandler"]; }
            set { this["errorHandler"] = value; }
        }                                                           
        #endregion


    }

    public class PoolingProviderConfigElement : ConfigurationElement
    {
        public PoolingProviderConfigElement()
        {

        }

        [ConfigurationProperty("name", DefaultValue = null, IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }
        #region +---- Pooling Conntroller ---------------------------
        [ConfigurationProperty("serviceType", DefaultValue = null, IsRequired = false)]
        public string ServiceType
        {
            get { return (string)base["serviceType"]; }
            set { base["serviceType"] = value; }
        }

        [ConfigurationProperty("poolEnabled", DefaultValue = true, IsRequired = false)]
        public bool PoolEnabled
        {
            get { return (bool)base["poolEnabled"]; }
            set { base["poolEnabled"] = value; }
        }
        [ConfigurationProperty("maxPoolSize", DefaultValue = 1048576, IsRequired = false)]
        public int MaxPoolSize
        {
            get { return (int)base["maxPoolSize"]; }
            set { base["maxPoolSize"] = value; }
        }
        [ConfigurationProperty("minPoolSize", DefaultValue = 0, IsRequired = false)]
        public int MinPoolSize
        {
            get { return (int)base["minPoolSize"]; }
            set { base["minPoolSize"] = value; }
        }

        [ConfigurationProperty("creationTimeout", DefaultValue = 60000, IsRequired = false)]
        public int CreationTimeout
        {
            get { return (int)base["creationTimeout"]; }
            set { base["creationTimeout"] = value; }
        }
        #endregion -------------------------------------------------




    }

    public class DispatchMessageInspectorConfigElement : ConfigurationElement
    {
        public DispatchMessageInspectorConfigElement()
        {

        }

        [ConfigurationProperty("name", DefaultValue = null, IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        //Declare the Schema collection property.
        //Note: the "IsDefaultCollection = false" instructs 
        //.NET Framework to build a nested section of 
        //the kind <Schema> ...</Schema>.
        [ConfigurationProperty("constructorAguments", IsDefaultCollection = true)]
        [ConfigurationCollection(typeof(ConstructorsCollection),
            AddItemName = "add",
            ClearItemsName = "clear",
            RemoveItemName = "remove")]
        public ConstructorsCollection ConstructorAguments
        {
            get
            {
                ConstructorsCollection ParametersCollection = (ConstructorsCollection)base["constructorAguments"];
                return ParametersCollection;
            }
        }

    }

    public class ErrorHandlerConfigElement : ConfigurationElement
    {
        public ErrorHandlerConfigElement()
        {

        }

        [ConfigurationProperty("name", DefaultValue = null, IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }
    }


    #region Constructor argument
    public class ConstructorConfigElement : ConfigurationElement
    {
        public ConstructorConfigElement()
        {

        }

        public Type InterfaceType { get { return Type.GetType(Interface); } }
        public Type ImplementType { get { return Type.GetType(Implement); } }

        [ConfigurationProperty("interface", DefaultValue = null, IsRequired = true, IsKey = true)]
        public string Interface
        {
            get { return (string)this["interface"]; }
            set { this["interface"] = value; }
        }

        [ConfigurationProperty("implement", DefaultValue = null, IsRequired = true, IsKey = false)]
        public string Implement
        {              
            get { return (string)this["implement"]; }
            set { this["implement"] = value; }
        }
    }

    public class ConstructorsCollection : ConfigurationElementCollection
    {
        public ConstructorsCollection()
        {

        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ConstructorConfigElement();
        }

        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((ConstructorConfigElement)element).Implement;
        }

        public ConstructorConfigElement this[int index]
        {
            get
            {
                return (ConstructorConfigElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public ConstructorConfigElement this[string Name]
        {
            get
            {
                return (ConstructorConfigElement)BaseGet(Name);
            }
        }

        public int IndexOf(ConstructorConfigElement Schema)
        {
            return BaseIndexOf(Schema);
        }

        public void Add(ConstructorConfigElement Schema)
        {
            BaseAdd(Schema);
        }
        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
        }

        public void Remove(ConstructorConfigElement Schema)
        {
            if (BaseIndexOf(Schema) >= 0)
                BaseRemove(Schema.Interface);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
        }
    }
    #endregion

}
